<?php
//商品板块管理
namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Requests;

class ForumController extends CommonController
{
    //商品板块管理浏览
    public function index()
    {
        $data = \DB::table("m_forum")->orderBy("id","asc")->paginate(7);
		return view("admin.forum.index",['list'=>$data]);
    }
	//商品板块添加操作界面
    public function create()
    {
        return view('admin.forum.add');
    }
	//商品板块后台添加
    public function store(Request $request)
    {
		$data = $request->all();
		$data = $request->except('_token');
		if(empty($data['name'])){
			$info = "请先输入商品板块信息！";
		}else{
			$d = \DB::table("m_forum")->where("name",$data['name'])->first();
			if(empty($d)){
				$id = \DB::table("m_forum")->insertGetId($data);
				$info = "商品板块添加成功！";
			}else{
				$info = "该商品板块已存在！";
			}
		}
		return redirect('admin/forum')->with("err",$info);
    }
    public function show($id)
    {
        //
    }
    public function edit($id)
    {
		$data = \DB::table("m_forum")->where('id',$id)->first();
		return view("admin.forum.edit",['list'=>$data]);
    }
    public function update(Request $request, $id)
    {
        $data =	$request->all();
		$data = $request->except('_token','_method');
		if(empty($data['name'])){
			$info = "修改值不能为空！";
		}else{
			$d = \DB::table("m_forum")->where("name",$data['name'])->first();
			if(empty($d)){
				\DB::table('m_forum')->where('id',$id)->update($data);
				$info = "商品板块修改成功！";
			}else{
				$info = "该商品板块已存在！";
			}
		}
		return redirect('admin/forum')->with("err",$info);
    }
	public function destroy($id)
    {
        $data = \DB::table("m_genre")->where('fid',$id)->get();
		//判断商品板块下是否没有子类别
		if(empty($data)){
			\DB::table('m_forum')->where('id', $id)->delete();
			$info = "商品板块删除成功！";
		}else{
            $info = "存在子类别，无法删除！";
        }
		return redirect('admin/forum')->with("err",$info);
    }
}
